package com.qsboss.demo.mq;

import com.alibaba.fastjson2.JSON;
import com.qsboss.demo.pojo.TbPrompt;
import com.qsboss.demo.service.TbPromptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.concurrent.*;

/**
 * @author 军哥
 * @version 1.0
 * @description: ActiveMQ 消息接收
 * @date 2023/10/28 22:25
 */

public class MyMessageListener implements MessageListener{

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    TbPromptService tbPromptService;

    private ThreadPoolExecutor threadPoolExecutor;

    public MyMessageListener() {
        //创建线程池
        BlockingDeque<Runnable> blockingDeque = new LinkedBlockingDeque<>();
        threadPoolExecutor = new ThreadPoolExecutor(2, 4, 3, TimeUnit.MINUTES, blockingDeque);
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    }

    @Override
    public void onMessage(Message message) {
        try {
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                String content = textMessage.getText();
                TbPrompt tbPrompt = JSON.parseObject(content, TbPrompt.class);

                System.out.println("发送催款邮件: =========================");
                System.out.println("发送催款邮件: " + content);
                System.out.println("发送催款邮件: =========================");

                // 存储到数据库
                tbPromptService.addPrompt(tbPrompt);

                // 使用线程池发送邮件
                Email email = new Email(javaMailSender, tbPrompt);
                Future<Boolean> res = threadPoolExecutor.submit(email);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


class Email implements Callable<Boolean> {

    private TbPrompt tbPrompt;
    private JavaMailSender javaMailSender;

    public Email(JavaMailSender sender, TbPrompt tbPrompt) {
        this.tbPrompt = tbPrompt;
        this.javaMailSender = sender;
    }

    @Override
    public Boolean call() throws Exception {
        Boolean res = send();//调用发送邮件的方法
        return res;
    }

    private Boolean send() {

        System.out.println(tbPrompt.getBillName());

        //-- 邮件发送密码
        try {
            //-- 创建邮件对象
            MimeMessage message = javaMailSender.createMimeMessage();

            //-- 创建helper
            MimeMessageHelper helper = new MimeMessageHelper(message);

            //-- 设置邮件的内容
            helper.setFrom("3350996729@qq.com");
            helper.setTo(tbPrompt.getCustomerMail());
            String title = "尊敬的" + tbPrompt.getCustomerName() + "用户，您的账单请查收";
            helper.setSubject(title);

            String body = "费用说明：" + tbPrompt.getBillName() + "<br>您的费用是：" + tbPrompt.getPayAmount() + "元。请尽快支付账单。<br>";

            helper.setText(body);

            //-- 发送邮件
            javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败，稍后再次发送");
            // 存储发送失败邮件信息
            // TODO
        } finally {
        }

        System.out.println("催收邮件发送成功~~~~~~~~~~~~~~~~~~~~~");

        return true;
    }


}